package com.songlanyun.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

/**
 * Created by zenghang on 2019/7/26.
 */
public class QueryWrapperUtil {


    /**
     * @param params
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQuery(Map<String, Object> params) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();

        for (String key : params.keySet()) {
            String[] c = key.split("_");
            if (c.length == 3) {
                if (c[0].equals("Q")) {
                    Object value = params.get(key);
                    //设置查询条件
                    switch (c[1]) {
                        case "EQ": //等于
                            queryWrapper.eq(c[2], value);
                            break;
                        case "NE": //不等于
                            queryWrapper.ne(c[2], value);
                            break;
                        case "GT": //大于
                            queryWrapper.gt(c[2], value);
                            break;
                        case "GE": //大于等于
                            queryWrapper.ge(c[2], value);
                            break;
                        case "LT": //小于
                            queryWrapper.lt(c[2], value);
                            break;
                        case "LE": //小于等于
                            queryWrapper.le(c[2], value);
                            break;
                        case "BET": //between
                            String[] betKey = value.toString().split("_");
                            queryWrapper.between(c[2], params.get(betKey[0]), params.get(betKey[1]));
                            break;
                        case "NBET": //not between
                            String[] nbetKey = value.toString().split("_");
                            queryWrapper.notBetween(c[2], params.get(nbetKey[0]), params.get(nbetKey[1]));
                            break;
                        case "LK": //like
                            queryWrapper.like(c[2], value);
                            break;
                        case "NLK": //not like
                            queryWrapper.notLike(c[2], value);
                            break;
                        case "LKL": //左 like
                            queryWrapper.likeLeft(c[2], value);
                            break;
                        case "LKR": //右 like
                            queryWrapper.likeRight(c[2], value);
                            break;
                        case "NU": //is null
                            queryWrapper.isNull(c[2]);
                            break;
                        case "NNU": //is not null
                            queryWrapper.isNotNull(c[2]);
                            break;
                        case "IN": //in
                            queryWrapper.in(c[2], value);
                            break;
                        case "NIN": //not in
                            queryWrapper.notIn(c[2], value);
                            break;
                        case "INS": //in sql
                            queryWrapper.inSql(c[2], String.valueOf(value));
                            break;
                        case "NINS": //not in sql
                            queryWrapper.notInSql(c[2], String.valueOf(value));
                            break;
                    }
                }
            } else if (c.length == 2) {
                //设置排序、分组、分页
                switch (c[0]) {
                    case "G":
                        queryWrapper.groupBy(c[1]);
                        break;
                    case "O":
                        if (c[1].toLowerCase().equals("asc"))
                            queryWrapper.orderByAsc(c[1]);
                        else if (c[1].toLowerCase().equals("desc"))
                            queryWrapper.orderByDesc(c[1]);
                        break;
                    case "P":
                        break;
                }
            }
        }
        return queryWrapper;
    }
}
